class Solution {
public:
    string addStrings(string num1, string num2) {
        string str;
        int n1 = num1.size() - 1, n2 = num2.size() - 1;
        int val1, val2, ans, ext = 0;
        while (n1 >= 0 || n2 >= 0)
        {
            val1 = n1 < 0 ? 0 : num1[n1--] - '0';
            val2 = n2 < 0 ? 0 : num2[n2--] - '0';
            int p = val1 + val2 + ext;
            ans = p % 10;
            str += ans + '0';
            ext = p / 10;
        }

        if (ext == 1)
            str += '1';
        reverse(str.begin(), str.end());


        return str;
    }
};